package com.lanou.pianke.piececomment.web.controller;
import com.github.pagehelper.PageHelper;
import com.lanou.pianke.piece.web.entity.Pcomment;
import com.lanou.pianke.piece.web.entity.Preplay;
import com.lanou.pianke.piece.web.pieceService.impl.PieceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class PcommentController {

    @Autowired
    private PieceService pieceService;

    //显示碎片评论和回复
    @ResponseBody
    @GetMapping("/piece/showPieceComment")
    public void showComment(Map map,String pieceId,
                            @RequestParam(defaultValue = "1") int pageNum,
                            @RequestParam(defaultValue = "10") int pageSize){
        List<Pcomment> pcomments = pieceService.selectCommentByPieceId(pieceId);
        for (Pcomment pcomment : pcomments) {
            String pcomment_id = pcomment.getPcomment_id();
            List<Preplay> preplay = pieceService.selectPreplayByPcommentId(pcomment_id);
            map.put("showPreplay",preplay);
        }
        PageHelper.startPage(pageNum,pageSize);
        map.put("showPcomment",pcomments);
    }

    //添加碎片评论--获取前端数据碎片id碎片内容text评论人uid
    @GetMapping("/pianke/addPieceComment")
    public void addPieceComment(Map map,String text,String pieceid,Long uid,String pieceText){
        Integer integer = pieceService.addPieceComment(pieceid,text,uid,pieceText);
        if (integer!=0){
            map.put("publish","发表成功");
        }else {
            map.put("publish","发表失败");
        }

    }

    //删除评论评论真删除  评论删除回复根据评论存在也就看不到
    @GetMapping("/pianke/delPieceComment")
    public void delPieceComment(String commentText){
        pieceService.delPieceCommentBycommentText(commentText);
    }


    //回复评论--获取评论内容，回复内容
    @GetMapping("/pianke/addReplay")
    public void addReplay(Map map,String commentText,String replayText,Long uid){
        Integer integer = pieceService.insertPreplay(commentText,replayText,uid);
        if (integer!=0){
            map.put("replay","回复成功");
        }else {
            map.put("replay","回复失败");
        }
    }


    //删除回复
    @GetMapping("/pianke/delReplay")
    public void delReplay(String replayText){
        pieceService.deleteReplayByReplayText(replayText);
    }

}
